package com.hzgj.bcl.util.lang;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StrKit {
	
	/**
	 * 首字母变小写
	 */
	public static String firstCharToLowerCase(String str) {
		char firstChar = str.charAt(0);
		if (firstChar >= 'A' && firstChar <= 'Z') {
			char[] arr = str.toCharArray();
			arr[0] += ('a' - 'A');
			return new String(arr);
		}
		return str;
	}
	
	/**
	 * 首字母变大写
	 */
	public static String firstCharToUpperCase(String str) {
		char firstChar = str.charAt(0);
		if (firstChar >= 'a' && firstChar <= 'z') {
			char[] arr = str.toCharArray();
			arr[0] -= ('a' - 'A');
			return new String(arr);
		}
		return str;
	}
	
	/**
	 * 字符串为 null 或者为  "" 时返回 true
	 */
	public static boolean isBlank(String str) {
		return str == null || "".equals(str.trim()) ? true : false;
	}
	
	/**
	 * 字符串不为 null 而且不为  "" 时返回 true
	 */
	public static boolean notBlank(String str) {
		return str == null || "".equals(str.trim()) ? false : true;
	}
	
	public static boolean notBlank(String... strings) {
		if (strings == null) {
            return false;
        }
		for (String str : strings) {
            if (str == null || "".equals(str.trim())) {
                return false;
            }
        }
		return true;
	}
	
	public static boolean notNull(Object... paras) {
		if (paras == null) {
            return false;
        }
		for (Object obj : paras) {
            if (obj == null) {
                return false;
            }
        }
		return true;
	}

    /**
     * 过滤Html中的Style、Script
     * 移植自NET\front_libs\Utility\Helper\StringHelper.cs
     * @param content
     * @return
     */
    public static String getCleanStyleHtmlText(String content)
    {
        if (isBlank(content)){
        	return "";
        }
        String s = content;

        //过滤<style>
        String styleRegex = "<style[\\s\\S]*?>([\\s\\S]*?<\\/style[\\s\\S]*?>)?";
        String styleSegmentRegex = "<style";
        //过滤<script>
        String scriptRegex = "<script[\\s\\S]*?>([\\s\\S]*?<\\/script[\\s\\S]*?>)?";
        String scriptSegmentRegex = "<script";
        
        s = replaceAllByRegex(styleRegex, s, "");
        s = replaceAllByRegex(styleSegmentRegex, s, "");
        s = replaceAllByRegex(scriptRegex, s, "");
        s = replaceAllByRegex(scriptSegmentRegex, s, "");

        return s;
    }
    
    private static String replaceAllByRegex(String regex, String content, String replace){
    	String s = content;
		Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(s);
		s= m.replaceAll(replace);
		
		return s;
    }

    /**
     * 移植.NET中的string.format（）函数
     * @param msg
     * @param params
     * @return
     */
    public static String formatMsg(String msg, Object... params) {
		for (int i = 0; i < params.length; i++) {
			msg = msg.replace("{" + i + "}", String.valueOf(params[i]));
		}
		return msg;
	}

	/**
	 * 比较两个字符串值是否相等，内部处理空引用。
	 *
	 * @param s1
	 * @param s2
	 * @return
	 */
	public static boolean equals(String s1, String s2) {
		if (s1 == s2) {
			return true;
		}
		if (s1 != null) {
			return s1.equals(s2);
		}
		return false;
	}
}




